<template>
  <div class="da">
    <div class="box clearfix">
      <div class="right">
        <div class="center">
          <p class="tit">智慧管理后台</p>
          <div class="inp">
            <el-form
              ref="loginForm"
              :model="loginForm"
              :rules="loginRules"
              class="login-form"
              autocomplete="on"
              label-position="left"
            >
              <el-form-item prop="username">
                <div class="svg-container">
                  <!--icon图标-->
                  <svg-icon icon-class="user" class-name="aa"/>
                </div>
                <el-input
                  ref="username"
                  v-model="loginForm.username"
                  placeholder="用户名"
                  name="username"
                  type="text"
                  tabindex="1"
                  autocomplete="on"
                />
              </el-form-item>

              <el-tooltip content="Caps lock is On" placement="right" manual>
                <el-form-item prop="password">
                  <div class="svg-container">
                    <svg-icon icon-class="password"/>
                  </div>
                  <el-input
                    :key="passwordType"
                    ref="password"
                    v-model="loginForm.password"
                    :type="passwordType"
                    placeholder="密码"
                    name="password"
                    tabindex="2"
                    autocomplete="on"
                  />
                  <span class="show-pwd" @click="showPwd">
                    <svg-icon
                      :icon-class="
                        passwordType === 'password' ? 'eye' : 'eye-open'
                      "
                    />
                  </span>
                </el-form-item>
              </el-tooltip>
            </el-form>
            <!-- <input
              type="text"
              placeholder="用户名"
              v-model="loginForm.username"
            />
            <input
              type="pass"
              placeholder="密码"
              v-model="loginForm.password"
            /> -->
            <div class="drag" ref="dragDiv">
              <div class="drag_bg"></div>
              <div class="drag_text">{{ confirmWords }}</div>
              <div
                ref="moveDiv"
                @mousedown="mousedownFn($event)"
                :class="{ handler_ok_bg: confirmSuccess }"
                class="handler handler_bg"
                style="position: absolute; top: 0px; left: 0px"
              ></div>
            </div>
            <div class="bottom">
              <div class="tiao">
                <p @click="goHome">登陆</p>
              </div>
            </div>
            <div class="fotter"><p>Copyright@2020</p></div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
// import axios from "axios";
export default {
  data() {
    const validateUsername = (rule, value, callback) => {
      if (value.length === 0) {
        callback(new Error("Please enter the correct user name"));
      } else {
        callback();
      }
    };
    const validatePassword = (rule, value, callback) => {
      if (value.length < 4) {
        callback(new Error("The password can not be less than 6 digits"));
      } else {
        callback();
      }
    };
    return {
      loginForm: {
        username: "",
        password: "",
      },
      loginRules: {
        username: [
          {required: true, trigger: "blur", validator: validateUsername},
        ],
        password: [
          {required: true, trigger: "blur", validator: validatePassword},
        ],
      },
      passwordType: "password",
      beginClientX: 0,
      mouseMoveStata: false,
      maxwidth: "",
      confirmWords: "请按住拖到最右边",
      confirmSuccess: false,
    };
  },
  mounted() {
    //默认input框聚焦
    if (this.loginForm.username === "") {
      this.$refs.username.focus();
    } else if (this.loginForm.password === "") {
      this.$refs.password.focus();
    }
    //滑动解锁
    this.maxwidth =
      this.$refs.dragDiv.clientWidth - this.$refs.moveDiv.clientWidth;
    document
      .getElementsByTagName("html")[0]
      .addEventListener("mousemove", this.mouseMoveFn);
    document
      .getElementsByTagName("html")[0]
      .addEventListener("mouseup", this.moseUpFn);
  },
  methods: {
    goHome() {
      // console.log(this.confirmSuccess);
      if (this.confirmSuccess) {
        this.$store.dispatch("user/login", this.loginForm).then(() => {
          if (localStorage.getItem("token")) {
            this.$router.push("/homePage")
          } else {
            this.$message.error('验证失败');
          }
        })
      } else {
        this.$message.error('验证失败');

      }
    },
    //密码显示隐藏
    showPwd() {
      if (this.passwordType === "password") {
        this.passwordType = "";
      } else {
        this.passwordType = "password";
      }
      this.$nextTick(() => {
        this.$refs.password.focus();
      });
    }
    ,
    mousedownFn: function (e) {
      //滑动解锁
      if (!this.confirmSuccess) {
        e.preventDefault && e.preventDefault(); //阻止文字选中等 浏览器默认事件
        this.mouseMoveStata = true;
        this.beginClientX = e.clientX;
      }
    }
    , //mousedoen 事件
    successFunction() {
      //滑动解锁
      this.confirmSuccess = true; //全局判断
      this.confirmWords = "验证通过";
      if (window.addEventListener) {
        document
          .getElementsByTagName("html")[0]
          .removeEventListener("mousemove", this.mouseMoveFn);
        document
          .getElementsByTagName("html")[0]
          .removeEventListener("mouseup", this.moseUpFn);
      } else {
        document
          .getElementsByTagName("html")[0]
          .removeEventListener("mouseup", () => {
          });
      }
      document.getElementsByClassName("drag_text")[0].style.color = "#fff";
      document.getElementsByClassName("handler")[0].style.left =
        this.maxwidth + "px";
      document.getElementsByClassName("drag_bg")[0].style.width =
        this.maxwidth + "px";
    }
    , //验证成功函数
    mouseMoveFn(e) {
      //滑动解锁
      if (this.mouseMoveStata) {
        let width = e.clientX - this.beginClientX;
        if (width > 0 && width <= this.maxwidth) {
          document.getElementsByClassName("handler")[0].style.left =
            width + "px";
          document.getElementsByClassName("drag_bg")[0].style.width =
            width + "px";
        } else if (width > this.maxwidth) {
          this.successFunction();
        }
      }
    }
    , //mousemove事件
    moseUpFn(e) {
      //滑动解锁
      this.mouseMoveStata = false;
      var width = e.clientX - this.beginClientX;
      if (width < this.maxwidth) {
        document.getElementsByClassName("handler")[0].style.left = 0 + "px";
        document.getElementsByClassName("drag_bg")[0].style.width = 0 + "px";
      }
    }
    , //mouseup事件
  },
};
</script>


<style>
.da .right .inp .el-input .el-input__inner {
  border: 0;
}
</style>
<style scoped lang="scss">
.login-form {
  margin-top: 24px;
  width: 100%;

  .el-form-item {
    width: 100%;
    border: 1px solid #eaeaea;
    text-align: center;
    border-radius: 4px;

    .el-form-item__content {
      width: 100%;

      .svg-container {
        width: 10%;
        float: left;
      }

      .el-input {
        width: 80%;
        float: left;
      }

      .show-pwd {
        display: inline-block;
        width: 10%;
        float: right;
      }
    }
  }
}

.fotter {
  margin-top: 230px;
}

.fotter p {
  font-size: 0.01em;
  text-align: center;
  color: #ccc;
  font-weight: 100;
}

.da {
  width: 100%;
  height: 100vh;
  background-color: rgba(34, 45, 50, 0.7);
}

.box {
  width: 35%;
  height: 640px;
  margin: 0 auto;
  padding-top: 20px;
}

/* .aa {
  float: left;
  font-size: 12px;
  margin-top: 18px;
  height: 35px;
  line-height: 35px;
} */
.right {
  width: 100%;
  height: 100%;
  float: left;
  background-color: #fff;
}

.tit {
  padding-top: 100px;
  text-align: center;
  font-size: 30px;
  color: rgba(34, 45, 50, 100);
}

.right .ying {
  font-size: 10px;
  color: rgba(58, 98, 215, 100);
  margin-top: 10px;
}

.center {
  width: 300px;
  margin: 0 auto;
}

// .inp > input {
//   outline: none;
//   width: 300px;
//   background-color: #fff;
//   border: 1px solid #eaeaea;
//   height: 44px;
//   margin-top: 24px;
//   text-indent: 10px;
//   font-size: 10px;
// }
.bottom {
  margin-top: 5px;
}

.bottom input {
  margin-top: 10px;
  float: left;
  margin-right: 5px;
}

.pass {
  float: left;
  font-size: 10px;
  padding-top: 8px;
}

.tiao {
  /* float: right; */
  width: 100%;
  height: 44px;
  background-color: rgba(34, 45, 50, 100);
  color: #fff;
  margin-top: 24px;
  border-radius: 3px;
  cursor: pointer;
}

.tiao p {
  text-align: center;
  line-height: 44px;
  font-size: 16px;
}

.drag {
  position: relative;
  background-color: #e8e8e8;
  width: 100%;
  height: 80px;
  line-height: 34px;
  margin-left: 0%;
  text-align: center;
}

.handler {
  width: 40px;
  height: 32px;
  border: 1px solid #ccc;
  cursor: move;
}

.handler_bg {
  background: #fff url("") no-repeat center;
}

.handler_ok_bg {
  background: #fff url("") no-repeat center;
}

.drag_bg {
  background-color: #7ac23c;
  height: 50px;
  width: 0px;
}

.drag_text {
  position: absolute;
  top: 0px;
  width: 100%;
  text-align: center;
  -moz-user-select: none;
  -webkit-user-select: none;
  user-select: none;
  -o-user-select: none;
  -ms-user-select: none;
  font-size: 14px;
}

.drag {
  position: relative;
  background-color: #e8e8e8;
  width: 100%;
  height: 47px;
  line-height: 47px;
  text-align: center;
  margin-top: 24px;
  /* margin-left: 62px; */
}

.handler {
  width: 60px;
  height: 47px;
  border: 1px solid #ccc;
  cursor: move;
}

.handler_bg {
  background: #fff url("") no-repeat center;
}

.handler_ok_bg {
  background: #fff url("") no-repeat center;
}

.drag_bg {
  background-color: #7ac23c;
  height: 47px;
  width: 0px;
}

.drag_text {
  position: absolute;
  top: 0px;
  width: 100%;
  text-align: center;
  -moz-user-select: none;
  -webkit-user-select: none;
  user-select: none;
  -o-user-select: none;
  -ms-user-select: none;
}
</style>
